<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {iframe} from "@/js/tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model_name_config = decode_config(_config('model_map'), {content_key:'label', value_key:'name'})
    let forms_config = {
      title: {label:'标题',name:'title',type:'text_one',tips:'活动标题'},
      start_time: {label:'开始时间',name:'start_time',type:'date',tips:''},
      end_time: {label:'结束时间',name:'end_time',type:'date',tips:''},
      user_ids: {label:'限制用户',name:'user_ids',type:'list_ids',config:'user/index',tips:'限制用户，不选为空表示不限制(即所有用户都可以参与)'},
      level: {label:'限制等级',name:'level',type:'checkbox',config:[],tips:'限制等级，不选为空表示不限制(即所有等级都可以参与)'},

      model_name: {label:'项目类型',name:'model_name',type:'select_one',config:model_name_config,tips:'限制项目类型，为空表示不限制(即所有类型的项目都可以参与)',value:''},
      limit_ids: {label:'项目ID集',name:'limit_ids',type:'text_one',tips:'限制项目ID集，为空表示不限制(即任意ID的项目都可以参与)',config:'',value:''},

      min_pay_money: {label:'满足金额',name:'min_pay_money',type:'text_one',config:'number',value:0,tips:'支付金额限制，指定参与活动需要的最低支付金额'},
      max_use: {label:'次数限制',name:'max_use',type:'text_one',config:'number',value:0,tips:'参与次数限制：0表示不限制。每个模型的参与次数独立计算，例如：次数限制1次，用户A以该促销购买过商品一次后，不能再使用该促销购买商品，但是可以以该促销购买课程或其他模型项目一次'},
      max_buy: {label:'仅首次购买',name:'max_buy',type:'radio_switch',value:0,tips:'是否仅首次购买可参与，设定为是，用户购买商品时，如果该商品之前购买过，那么就不能使用该促销购买'},
      reduction_type: {label:'减免方式',name:'reduction_type',type:'select_one',config:['减少固定金额','折扣','降低到指定金额'], value:0, tips:''},
      reduction_value: {label:'减免值',name:'reduction_value',type:'text_one',config:'number',tips:'填写减少的金额',value:''},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/promotion/edit?id=`+this.id : '/admin/promotion/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('promotion/index', '$refs.list_url.list');
          iframe.hide('promotion/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
  },
  watch: {
    'config.reduction_type.value'(v, oldV){
      let tips_map = ['填写减少的金额','填写折扣比例：1-99，90表示9折','填写项目的最终金额'];
      this.config.reduction_value.tips = tips_map[v];
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/promotion/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('promotion/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    ajax({url:'/admin/user_level/list', cache:true}, (res)=>{
      this.config.level.config = decode_config(res.data.list, {content_key:'name', value_key:'level'});
      return false
    })
  }
}
</script>
